package com.pccw.ssh.manager;

import java.util.ArrayList;
import java.util.List;

import com.pccw.ssh.dao.StockDao;
import com.pccw.ssh.domain.Stock;
import com.pccw.ssh.service.StockService;
import com.pccw.ssh.vo.StockVO;

public class StockServiceImp implements StockService {
	
	private StockDao stockDao;
	
	public StockDao getStockDao() {
		return stockDao;
	}

	public void setStockDao(StockDao stockDao) {
		this.stockDao = stockDao;
	}

	public void add(StockVO stockVO) {
        stockDao.add(new Stock(stockVO));
	}

	public void delete(StockVO stockVO) {
		 stockDao.delete(new Stock(stockVO));
	}

	public StockVO getByPK(String num) {
		Stock stock = stockDao.getByPK(num);
		return new StockVO(stock.getNum(),stock.getName(),stock.getPrice());
	}

	public void update(StockVO stockVO) {
		stockDao.update(new Stock(stockVO));
	}

	public List<StockVO> getByAttribute(String attributeName,
			String attributeValue) {
		return toVO(stockDao.getByAttribute(attributeName, attributeValue));
	}

	public List<StockVO> getByVO(StockVO stockVO) {
		return toVO(stockDao.getByPO(new Stock(stockVO)));
	}
	
	private List<StockVO> toVO(List<Stock> stocks){
		List<StockVO> stockVOs = new ArrayList<StockVO>();
		for(Stock s :stocks){
			StockVO stockVO = new StockVO(s.getNum(),s.getName(),s.getPrice());
			stockVOs.add(stockVO);
		}
		return stockVOs;
	}
	private StockVO toVO(Stock stock ){

		StockVO stockVO = new StockVO(stock.getNum(),stock.getName(),stock.getPrice());
		return stockVO;
	}
}
